<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://115cms.com.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 作者@梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Api extends Controller
{

    //发送验证码
    public function yanzhengma()
    {
        hook('phpapiyanzhengma');
        // 判断是否是数字 是数字就默认跳转电话注册
        if (is_numeric(input('name'))){
            //手机号码注册
            if (!preg_match("/^1[34578]{1}\\d{9}\$/", input('name'))) {
                $this->error('手机号码或者邮箱格式错误');
            }
            $phone = Db::name('member')->where('phone', input('name'))->find();
            if ($phone) {
                $this->error('当前手机号码被注册');
            }
            $code = mt_rand(100000, 999999);
            //查询当前时间该验证码是否过期
            //输出当天最新验证码过期时间
            $times = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 1, 'tid' => 1))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if (fun_tj('yanzhengma', 'time', Date_k(0), 'time', Date_k(1), array('open' => 1)) > set('dxb_ip')) {
                $this->error('您当前发送次数过多');
            }
            if ($times >= time()) {
                $this->error('验证码还未过期');
            } else {
                //发送短信
                $content = preg_replace("/\{M}/is", $code, set('dxb_title'));
                duanxin(input('name'), $content);
                $data['ma'] = $code;
                $data['phone'] = input('name');
                $data['mail'] = '';
                $data['content'] = $content;
                $data['open'] = 1;
                $data['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['time'] = time();
                $data['fl'] = 1;
                $data['tid'] = 1;
                $data['times'] = time() + 300;
                Db::name('yanzhengma')->insert($data);
                $this->success('发送成功');
            }
        } else {
            //邮箱注册
            if (!filter_var(input('name'), FILTER_VALIDATE_EMAIL)) {
                $this->error('手机号码或者邮箱格式错误');
            }
            $phone = Db::name('member')->where('usermail', input('name'))->find();
            if ($phone) {
                $this->error('当前邮箱被注册');
            }
            //查询当前时间该验证码是否过期
            //输出当天最新验证码过期时间
            $times = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 1, 'tid' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if ($times >= time()) {
                $this->error('验证码还未过期');
            } else {
                $code = mt_rand(100000, 999999);
                $content = preg_replace("/\{M}/is", $code, set('mail_reg'));
                $data['ma'] = $code;
                $data['mail'] = input('name');
                $data['phone'] = '';
                $data['content'] = $content;
                $data['open'] = 1;
                $data['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['time'] = time();
                $data['fl'] = 1;
                $data['tid'] = 2;
                $data['times'] = time() + 300;
                Db::name('yanzhengma')->insert($data);
                //发送邮件
                send_mail(input('name'), '【' . set('title') . '】注册验证', $content);
                $this->success('发送成功');
            }
        }
    }
    //提交登录
    public function login()
    {
        hook('phpapilogin');
        if (session('userid') != '') {
            $this->error('您已登录');
        }
        $member = Db::name('member');
        if (request()->isPost()) {
            $data = input('post.');
            $open['open'] = 1;
            //手机邮箱方可登录
            $map['phone|usermail'] = $data['name'];
            $key = 'www.guojicms.com';
            $key1 = 'www.115cms.com';
            $user = Db::name('member')->where($open)->where($map)->find();
            if ($user) {
                if (jiami($user['password'], 'DECODE', $key, 0) == $data['password']||jiami($user['password'], 'DECODE', $key1, 0) == $data['password']||$user['password'] == md5($data['password'])) {
                    session('phone', $user['phone']);
                    session('userid', $user['userid']);
                    session('usermail', $user['usermail']);
                    $member->where($map)->setField(array('last_login_ip' => $_SERVER['REMOTE_ADDR'], 'last_login_time' => time()));
                    //登录送积分
                    point(set('jifen2', 2), session('userid'), '登录');
                    $this->success('登录成功');
                } else {
                    $this->error('密码错误');
                }
            } else {
                $this->error('账号不存在 或者异常');
            }
        }
    }
    //提交注册
    public function reg()
    {
        hook('phpapireg');
        if (session('userid') != '') {
            $this->error('当前处于登录状态');
        }
        $member = Db::name('member');
        if (filter_var(input('name'), FILTER_VALIDATE_EMAIL)) {
            if (request()->isPost()) {
                $password = input('password');
                $key = 'www.guojicms.com';
                $map['usermail'] = input('name');
                $mail = $member->where($map)->find();
                //查询当前手机号码对应的验证码
                //查询过期时间
                $times = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 1, 'tid' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
                if ($times >= time()) {
                    $ma = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 1, 'tid' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
                }
                if (strlen(input('yanzhengma'))<6) {
                    $this->error('请输入正确的验证码');
                }
                if ($ma != input('yanzhengma')) { 
                    $this->error('验证码错误');
                }
                if (!$mail) {
                    if (!filter_var(input('name'), FILTER_VALIDATE_EMAIL)) {
                        $this->error('邮箱格式错误');
                    }
                    $data['regtime'] = time();
                    $data['last_login_time'] = time();
                    $data['mailopen'] = 0;
                    $data['dengji'] = 0;
                    $data['open'] = 1;
                    $data['usermail'] = input('name');
                    $data['phone'] = '';
                    $data['username'] = '用户'.generate_password(8);
                    $data['userhead'] = '/public/img/touxiang.jpg';
                    $data['userip'] = $_SERVER['REMOTE_ADDR'];
                    $data['password'] = jiami($password, 'ENCODE', $key, 0);
                    if ($member->strict(false)->insert($data)) {
                        $getid = Db::name('member')->where($map)->find();
                        //注册积分
                        point(set('jifen1', 2), $getid['userid'], '注册');
                        session('phone', $getid['phone']);
                        session('userid', $getid['userid']);
                        session('usermail', $getid['usermail']);
                        //登录送积分
                        point(set('jifen2', 2), session('userid'), '登录');
                        $this->success('注册成功');
                    } else {
                        $this->error('注册失败');
                    }
                } else {
                    $this->error('该邮箱已存在');
                }
            }        
                    
        }else {
            // 手机注册
            if (request()->isPost()) {
                $password = input('password');
                $key = 'www.guojicms.com';
                $map['phone'] = input('name');
                $mail = $member->where($map)->find();
                //查询当前手机号码对应的验证码
                //查询过期时间
                $times = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 1, 'tid' => 1))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
                if ($times >= time()) {
                    $ma = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 1, 'tid' => 1))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
                }
                if (strlen(input('yanzhengma'))<6) {
                    $this->error('请输入正确的验证码');
                }
                if ($ma != input('yanzhengma')) {
                    $this->error('验证码错误');
                }
                if (!$mail) {
                    $user = $member->where('usermail', $data['usermail'])->find();
                    if (!preg_match("/^1[34578]{1}\\d{9}\$/", input('name'))) {
                        $this->error('手机号码格式错误');
                    }
                    $data['regtime'] = time();
                    $data['last_login_time'] = time();
                    $data['mailopen'] = 0;
                    $data['dengji'] = 0;
                    $data['open'] = 1;
                    $data['usermail'] = '';
                    $data['phone'] = input('name');
                    $data['username'] = '用户'.generate_password(8);
                    $data['userhead'] = '/public/img/touxiang.jpg';
                    $data['userip'] = $_SERVER['REMOTE_ADDR'];
                    $data['password'] = jiami($password, 'ENCODE', $key, 0);
                    if ($member->strict(false)->insert($data)) {
                        $getid = Db::name('member')->where($map)->find();
                        //注册积分
                        point(set('jifen1', 2), $getid['userid'], '注册');
                        session('phone', $getid['phone']);
                        session('userid', $getid['userid']);
                        session('usermail', $getid['usermail']);
                        //登录送积分
                        point(set('jifen2', 2), session('userid'), '登录');
                        $this->success('注册成功');
                    } else {
                        $this->error('注册失败');
                    }
                } else {
                    $this->error('该手机号码已存在');
                }
            }
        }
    }
    //找回发送验证码
    public function yanzhengmazh()
    {
        hook('phpapiyanzhengma');
        if (is_numeric(input('name'))){
            //手机号码
            if (!preg_match("/^1[34578]{1}\\d{9}\$/", input('name'))) {
                $this->error('手机号码格式错误');
            }
            $phone = Db::name('member')->where('phone', input('name'))->find();
            if (!$phone) {
                $this->error('手机号码不存在');
            }
            //查询当前时间该验证码是否过期
            //输出当天最新验证码过期时间
            $times = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 2, 'tid' => 1))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if (fun_tj('yanzhengma', 'time', Date_k(0), 'time', Date_k(1), array('open' => 1, 'fl' => 2, 'tid' => 1)) > set('dxb_ip')) {
                $this->error('您当前发送次数过多');
            }
            if ($times >= time()) {
                $this->error('验证码还未过期');
            } else {
                $code = mt_rand(100000, 999999);
                $content = preg_replace("/\{M}/is", $code, set('dxb_title1'));
                //发送短信
                duanxin(input('name'), $content);
                $data['ma'] = $code;
                $data['phone'] = input('name');
                $data['content'] = $content;
                $data['open'] = 1;
                $data['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['time'] = time();
                $data['fl'] = 2;
                $data['tid'] = 1;
                $data['times'] = time() + 300;
                Db::name('yanzhengma')->insert($data);
                $this->success('发送成功');
            }
        } else {
            if (!filter_var(input('name'), FILTER_VALIDATE_EMAIL)) {
                $this->error('邮箱格式错误');
            }
            $usermail = Db::name('member')->where('usermail', input('name'))->find();
            if (!$usermail) {
                $this->error('邮箱不存在');
            }
            //查询当前时间该验证码是否过期
            //输出当天最新验证码过期时间
            $times = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if ($times >= time()) {
                $this->error('验证码还未过期');
            } else {
                $code = mt_rand(100000, 999999);
                $content = preg_replace("/\{M}/is", $code, set('mial_zhaohui'));
                //发送邮箱
                send_mail(input('name'), '【' . set('title') . '】找回密码验证', $content);
                $data['ma'] = $code;
                $data['mail'] = input('name');
                $data['content'] = $content;
                $data['open'] = 1;
                $data['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['time'] = time();
                $data['fl'] = 2;
                $data['tid'] = 2;
                $data['times'] = time() + 300;
                Db::name('yanzhengma')->insert($data);
                $this->success('发送成功');
            }
        }
    }
    //找回密码手机找回
    public function retrieve()
    {
        hook('phpapiretrieve');
        $member = Db::name('member');
        if (request()->isPost()) {
            $data = input('post.');
            $password = input('password');
            $key = 'www.guojicms.com';
            //不能随便更改会导致用户无法登录
            if (is_numeric(input('name'))){
                // 手机
                $times = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
                if ($times >= time()) {
                    $ma = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
                }
                if (strlen(input('yanzhengma'))<6) {
                    $this->error('请输入正确的验证码');
                }
                $map['phone'] = $data['name'];
                $mail = $member->where($map)->find();
                if (!$mail) {
                    return json(array('code' => 0, 'msg' => '手机号码不存在'));
                } else {
                    if ($ma != input('yanzhengma')) {
                        $this->error('验证码错误');
                    }
                    $datam['password'] = jiami($password, 'ENCODE', $key, 0);
                    if ($member->where(array('phone' => input('name')))->update($datam)) {
                        Db::name('yanzhengma')->where(array('phone' => input('name')))->setField(array('open' => 0));
                        $this->success('密码重置成功');
                    } else {
                        $this->error('密码重置失败');
                    }
                } 
            } else {
                // 邮箱
                $times = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
                if ($times >= time()) {
                    $ma = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
                }
                $map['usermail'] = $data['name'];
                $mail = $member->where($map)->find();
                if (strlen(input('yanzhengma'))<6) {
                    $this->error('请输入正确的验证码');
                }
                if (!$mail) {
                    return json(array('code' => 0, 'msg' => '邮箱不存在'));
                }  else {
                    if ($ma != input('yanzhengma')) {
                        $this->error('验证码错误.');
                    }
                    $datam['password'] = jiami($password, 'ENCODE', $key, 0);
                    if ($member->where(array('usermail' => input('name')))->update($datam)) {
                        Db::name('yanzhengma')->where(array('mail' => input('name')))->setField(array('open' => 0));
                        $this->success('密码重置成功');
                    } else {
                        $this->error('密码重置失败');
                    }
                }
            }  
             
        }
    }
    // 退出
    public function logout()
    {
        hook('phpapilogout');
        session("userid", NULL);
        session("phone", NULL);
        $this->success('退出成功','/');
        return NULL;
    }
     
    //基本资料修改
    public function user_set()
    {
        hook('phpapiuserset');
        if (!session('userid')) {
            $this->error('请登录');
        } else {
            $member = Db::name('member');
            $uid = session('userid');
            $key = 'www.guojicms.com';
            if (request()->isPost()) {
                $data = $this->request->post();
                $_data['username'] = get('username',generate_password(10));
                if (mb_strlen($_data['username'], 'UTF-8') > 15) {
                    $this->error('用户名不能大于15个字符');
                }
                $_data['userhome'] =  xss(get('userhome','中国'));
                $_data['description'] =  xss(get('description',''));
                $_data['sex'] = get('sex','');
                $_data['sm_id'] = get('sm_id','');
                $_data['sm_at'] = get('sm_at','');
                $_data['userhead'] = get('userhead');
                if (!empty(input('passwords')||!empty(input('password')))) {
                    $password = input('password');
                    if (input('password') == input('passwords')) {
                        $_data['password'] = jiami($password, 'ENCODE', $key, 0);
                    } else {
                        return json(array('code' => 0, 'msg' => '两次输入的密码不一致'));
                    }
                }
                if ($member->where(array('userid' => $uid))->update($_data)) {
                    return json(array('code' => 1, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
        }
    }
    //手机更换验证码
    public function yanzhengma_genghuan()
    {
        hook('phpapiyanzhenghmagenghuan');
        if (is_numeric(input('name'))){
            //手机
            if (!preg_match("/^1[34578]{1}\\d{9}\$/", input('name'))) {
                $this->error('手机号码或者邮箱格式错误');
            }
            $phone = Db::name('member')->where('phone', input('name'))->find();
            if ($phone) {
                $this->error('当前手机号码被注册');
            }
            $code = mt_rand(100000, 999999);
            //查询当前时间该验证码是否过期
            //输出当天最新验证码过期时间
            $times = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 3, 'tid' => 1))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if (fun_tj('yanzhengma', 'time', Date_k(0), 'time', Date_k(1), array('open' => 1)) > set('dxb_ip')) {
                $this->error('您当前发送次数过多');
            }
            if ($times >= time()) {
                $this->error('验证码还未过期');
            } else {
                //发送短信
                $content = preg_replace("/\{M}/is", $code, set('dxb_title2'));
                duanxin(input('name'), $content);
                $data['ma'] = $code;
                $data['phone'] = input('name');
                $data['content'] = $content;
                $data['open'] = 1;
                $data['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['time'] = time();
                $data['fl'] = 3;
                $data['tid'] = 1;
                $data['times'] = time() + 300;
                Db::name('yanzhengma')->insert($data);
                $this->success('发送成功');
            }
        } else {
            // 邮箱
            if (!filter_var(input('name'), FILTER_VALIDATE_EMAIL)) {
                $this->error('手机号码或者邮箱格式错误');
            }
            $phone = Db::name('member')->where('usermail', input('name'))->find();
            if ($phone) {
                $this->error('当前邮箱被注册');
            }
            //查询当前时间该验证码是否过期
            //输出当天最新验证码过期时间
            $times = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 3, 'tid' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if ($times >= time()) {
                $this->error('验证码还未过期');
            } else {
                $code = mt_rand(100000, 999999);
                $content = preg_replace("/\{M}/is", $code, set('mail_bangding'));
                $data['ma'] = $code;
                $data['mail'] = input('name');
                $data['content'] = $content;
                $data['open'] = 1;
                $data['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['time'] = time();
                $data['fl'] = 3;
                $data['tid'] = 2;
                $data['times'] = time() + 300;
                Db::name('yanzhengma')->insert($data);
                //发送邮件
                send_mail(input('name'), '【' . set('title') . '】更换账号证码', $content);
                $this->success('发送成功');
            }
        }
    }
    //账号更换邮箱及手机
    public function user_set_name()
    {
        hook('phpapiusersetname');
        if (!session('userid')) {
            $this->error('请登录');
        } 
        if (is_numeric(input('name'))){
            $member = Db::name('member');
            if (request()->isPost()) {
                $mail = $member->where(array('userid' => user('userid')))->find();
                //查询当前手机号码对应的验证码
                //查询过期时间
                $times = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 3))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
                if ($times >= time()) {
                    $ma = Db::name('yanzhengma')->where(array('phone' => input('name'), 'fl' => 3))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
                }
                if (strlen(input('yanzhengma'))<6) {
                    $this->error('请输入正确的验证码');
                }
                if ($ma != input('yanzhengma')) {
                    $this->error('验证码错误');
                }
                $_data['phone'] = get('name');
                if ($member->where(array('userid' => user('userid')))->update($_data)) {
                    return json(array('code' => 1, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
        } else {
            // 邮箱
            $member = Db::name('member');
            if (request()->isPost()) {
                $mail = $member->where(array('userid' => user('userid')))->find();
                //查询当前手机号码对应的验证码
                //查询过期时间
                $times = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 3))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
                if ($times >= time()) {
                    $ma = Db::name('yanzhengma')->where(array('mail' => input('name'), 'fl' => 3))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
                }
                if (strlen(input('yanzhengma'))<6) {
                    $this->error('请输入正确的验证码');
                }
                if ($ma != input('yanzhengma')) {
                    $this->error('验证码错误..');
                }
                $_data['usermail'] = get('name');
        
                if ($member->where(array('userid' => user('userid')))->update($_data)) {
                    return json(array('code' => 1, 'msg' => '修改成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '修改失败'));
                }
            }
        }
    }
    //用户提现
    public function tixian_user()
    {
        hook('phpapitixianuser');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (get('jiage') < set('txpoint',2)) {
            $this->error('提现'.set('jifen',2).'不能少于'.set('txpoint',2).'');
        }
        if(!preg_match('/^[0-9]{1,}$/',get('jiage'))){
            $this->error('必须是数字');
        }
        if (!is_numeric(get('jiage'))||strpos(get('jiage'),".")!==false) {
            $this->error('必须是整数');
        }
        if (get('jiage')>user('point')) {
            $this->error('提现不能大于现有的'.set('jifen',2).'');
        }
        if (empty(user('sm_id'))||empty(user('sm_at'))) {
            $this->error('未设置提现账号和名字');
        }
            $data['uid'] = session('userid');
            $data['ip'] = $_SERVER['REMOTE_ADDR'];
            $data['name'] = user('sm_id');
            $data['zhanghu'] = user('sm_at');
            $data['jifen'] = get('jiage');
            $data['rmb'] = get('jiage')/set('txjf',2);
            $data['open'] = 0;
            $data['time'] = time();
            if (Db::name('tixian')->insert($data)) {
                //扣除积分
                point(-get('jiage'), session('userid'), '申请提现');
                return json(array('code' => 1,'msg' => '申请提现成功'));
            } else {
                $this->error('申请失败');
            }

    }
    // 付费
    public function fufei()
    {
        hook('phpapifufei');
        if (!session('userid')) {
            $this->error('未登录');
        }
        $t = Db::name(get('m','html'))->where(array('id' => input('id'), 'open' => 1))->find();
        if(fun_st('point_note',array('title'=>'付费阅读'.get('m','html'),'tid'=>$t['id'],'uid'=>user('userid')))!=0){
            $this->error('不需要重复扣费');
        }
        //扣除积分
        point(-$t['jifen'], session('userid'), '付费阅读'.get('m','html'),$t['id']);
        // 给作者添加积分
        point($t['jifen'], $t['uid'], '内容收入'.$t['title'],$t['id']);
        $this->success('付费成功');
    }
    
    //独立输出文章内容
    public function at()
    {
        hook('phpapiat');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (input('id')) {
            $c = Db::name(get('m'))->find(get('id',0));
            echo $c['content'];
        }
    }
    //重置内容获取
    public function cz()
    {
        hook('phpapicz');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (input('id')) {
            $c = Db::name(get('m'))->find(get('id',0));
            return json(array('code' => 1, 'msg' => '获取成功' ,'com'=>$c['content']));
        }
    }
    //通用删除
    public function deletes()
    {
        hook('phpapideletes');
        if (!session('userid')) {
            $this->error('未登录');
        }
        $id = input('id');
        if (fun_sc_p(input('model'), array('id' => $id), 'uid') != session('userid')) {
            $this->error('参数不全');
        }
        if (Db::name(input('model'))->delete(array('id' => $id, 'uid' => session('userid')))) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //通用更新内容
    public function biaoji()
    {
        hook('phpapibiaoji');
        if (!session('userid')) {
            $this->error('未登录');
        }
        $id = input('id');
        if (fun_sc_p(input('model'), array('id' => $id), 'uid') != session('userid')) {
            $this->error('参数不全');
        }
        $id = input('id');
        $data['open']=get('open');
        if (Db::name(ps(input('model')))->strict(false)->where(array('id' => $id))->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
     //收藏
    public function shoucang()
    {
        hook('phpapishoucang');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (fun_st('shoucang',array('tid'=>input('id'),'m'=>get('m','html')))>0) {
            Db::name('shoucang')->where(array('tid'=>input('id'),'m'=>get('m','html')))->delete();
            $shoucang=fun_st('shoucang',array('tid'=>input('id'),'m'=>get('m','html')));
            //更新收藏统计
            Db::name(get('m','html'))->where(array('id' => input('id')))->update(array('shoucang'=>dingyi($shoucang,0)));
            $this->error('取消收藏成功');
        } else {
            Db::name('shoucang')->insert(array('tid'=>input('id'),'time'=>time(),'uid'=>user('userid'),'m'=>get('m','html')));
            $shoucang=fun_st('shoucang',array('tid'=>input('id'),'m'=>get('m','html')));
            //更新收藏统计
            Db::name(get('m','html'))->where(array('id' => input('id')))->update(array('shoucang'=>dingyi($shoucang,0)));
            $this->success('收藏成功');
        }
      
    }
     //点赞
    public function zan()
    {
        hook('phpapizan');
        $zan=input('m').input('id');
        if (cookie('zan')==$zan) {
            $this->error('不能重复点赞');
        }
        $c = Db::name(input('m'))->where(array('id' => input('id')))->find();
        if (Db::name(input('m'))->strict(false)->where(array('id' => input('id')))->update(array('zan'=>$c['zan']+1))) {
            cookie('zan',$zan);
            $this->success('点赞成功');
        } else {
            $this->error('点赞失败');
        }
      
    }
    //发布评论
    public function comment_add()
    {
        hook('phpapicommentadd');
        if (!session('userid')) {
            $this->error('请先登录');
        } else {
            $html = Db::name('comment');
            $id = get('id');
            $time = Db::name('comment')->where(array('uid' => session('userid')))->order('id desc')->value('time');
          
            if ($time+set('plgs',8) >= time()) {
                $this->error(set('plgs',8).'秒后方可继续评论');
            }
            $data = $this->request->post();
            $_data['fid'] = $id;
            $_data['time'] = time();
            $_data['uid'] = session('userid');
            $_data['content'] = xss(input('content'));
            $_data['tid'] = get('tid','0');
            $_data['m'] = get('m','html');
            if (empty(input('content'))) {
                $this->error('内容不能为空');
            }
            if (mb_strlen(input('content'), 'UTF-8')>set('pinglunxz',8)) {
                $this->error('评论内容字数不能大于'.set('pinglunxz',8));
            }
            if (!$html->data($_data)->insert()) {
                $this->error('评论失败');
            } else {
                //添加积分记录评论者
                point(set('pla',2), session('userid'), '评论');
                //添加积分记录文章作者
                point(set('pls',2), fun_sc_p('html', array('id'=>$id), 'uid'), '文章被评论');
                $pinglun=fun_st('comment',array('open'=>1,'fid'=>$id,'m'=>$_data['m']));
                //更新评论统计
                Db::name(get('m','html'))->where(array('id' => input('id')))->update(array('pinglun'=>dingyi($pinglun,0)));
                $this->success('评论成功');
            }
        }
    }
    //添加内容
    public function add()
    {
        hook('phpapiadd');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (user('open')==0) {
            $this->error('账号被禁用');
        }
        $cate = Db::name('cate')->where(array('id' => input('tid')))->find();
        if ($cate['tougao']!=1) {
            $this->error('请选择正确的栏目');
        }
        $time = Db::name(input('model'))->where(array('uid' => session('userid')))->order('id desc')->value('time');
        if ($time+set('ztjg',8) >= time()) {
            $this->error(set('ztjg',8).'秒后方可继续发布');
        }
        $data['open'] = 1;
        $cars =explode(',', set('gjz',8));
        foreach($cars as $x=>$v){
            if (strpos(input('content'),$v) !== false) {
                $data['open'] = 0;    
            } 
        }
        $da = $this->request->post();
        $data['time'] = time();
        $data['times'] = time();
        $data['tid'] = xss($da['tid']);
        $data['jifen'] = xss($da['jifen']);
        $data['title'] = xss($da['title']);
        $data['uid'] = user('userid');
        $data['content'] = $da['content'];
        if (config_m(muban(), 'add')) {
            $carq= config_m(muban(),'add');
            foreach($carq as $x=>$v){
                $data[''.$x.''] = xss($da[$x]);
            }
        } 
        if (!Db::name(ps(input('model')))->strict(false)->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //内容更新
    public function edits()
    {
        hook('phpapiedits');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (user('open')==0) {
            $this->error('账号被禁用');
        }
        $cate = Db::name('cate')->where(array('id' => input('tid')))->find();
        if ($cate['tougao']!=1) {
            $this->error('请选择正确的栏目');
        }
        $cars =explode(',', set('gjz',8));
        foreach($cars as $x=>$v){
            if (strpos(input('content'),$v) !== false) {
                $data['open'] = 0;    
            } 
        }
        $da = $this->request->post();
        $data['times'] = time();
        $data['tid'] = xss($da['tid']);
        $data['jifen'] = xss($da['jifen']);
        $data['title'] = xss($da['title']);
        $data['content'] = $da['content'];
        if (config_m(muban(), 'add')) {
            $carq= config_m(muban(),'add');
            foreach($carq as $x=>$v){
                $data[''.$x.''] = xss($da[$x]);
            }
        } 
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $da['id'])->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //添加内容模型
    public function add_model()
    {
        hook('phpapiaddmodel');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (user('open')==0) {
            $this->error('账号被禁用');
        }
        $cate = Db::name('cate_m')->where(array('id' => input('tid')))->find();
        if ($cate['tougao']!=1) {
            $this->error('当前栏目禁止投稿');
        }
        $time = Db::name(input('model'))->where(array('uid' => session('userid')))->order('id desc')->value('time');
        if ($time+set('ztjg',8) >= time()) {
            $this->error(set('ztjg',8).'秒后方可继续发布');
        }
        $data['open'] = 1;
        $cars =explode(',', set('gjz',8));
        foreach($cars as $x=>$v){
            if (strpos(input('content'),$v) !== false) {
                $data['open'] = 0;    
            } 
        }
        $da = $this->request->post();
        $data['time'] = time();
        $data['times'] = time();
        $data['tid'] = xss($da['tid']);
        $data['jifen'] = xss($da['jifen']);
        $data['title'] = xss($da['title']);
        $data['uid'] = user('userid');
        $data['content'] = $da['content'];
        $carq= json_model(fun_model(input('model'), 'config'));
        foreach($carq as $x=>$v){
            $data[''.json_model_v($v,'zhiduan').''] = xss($da[json_model_v($v,'zhiduan')]);
        }
        if (!Db::name(ps(input('model')))->strict(false)->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //内容更新
    public function edits_model()
    {
        hook('phpapiedits');
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (user('open')==0) {
            $this->error('账号被禁用');
        }
        $cate = Db::name('cate_m')->where(array('id' => input('tid')))->find();
        if ($cate['tougao']!=1) {
            $this->error('请选择正确的栏目');
        }
        $cars =explode(',', set('gjz',8));
        foreach($cars as $x=>$v){
            if (strpos(input('content'),$v) !== false) {
                $data['open'] = 0;    
            } 
        }
        $da = $this->request->post();
        $data['times'] = time();
        $data['tid'] = xss($da['tid']);
        $data['jifen'] = xss($da['jifen']);
        $data['title'] = xss($da['title']);
        $data['content'] = $da['content'];
        $carq= json_model(fun_model(input('model'), 'config'));
        if (fun_model(input('model'), 'config')) {
            foreach($carq as $x=>$v){
                $data[''.json_model_v($v,'zhiduan').''] = xss($da[json_model_v($v,'zhiduan')]);
            }
        } 
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $da['id'])->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
}